/****************************************************************************
**
** Copyright (C) 2011 Kimitake
** Contact: Kimitake (kimitake@gmail.com)
**
** This file is part of CuteUp application.
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General
** Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** This file is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without evene the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this file. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include <QtCore/QCoreApplication>
#include <QtDeclarative>

#include "cuteup.h"

#include <QDebug>

#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK

CuteUp::CuteUp(QWidget *parent)
    : QMainWindow(parent)
{
	view = new QDeclarativeView(this);
	view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
	setCentralWidget(view);
	engine = view->engine();
	context = engine->rootContext();
	context->setContextProperty(QLatin1String("meetup"), this);
#if defined(Q_WS_MAEMO_5)
	context->setContextProperty(QLatin1String("fontSize"), "16");
#elif defined(Q_OS_SYMBIAN)
	context->setContextProperty(QLatin1String("fontSize"), "6");
#else
    context->setContextProperty(QLatin1String("fontSize"), "10");
#endif
	context->setContextProperty(QLatin1String("screenWidth"), width());
    context->setContextProperty(QLatin1String("screenHeight"), height());
//	qmlRegisterType<EventInfo>("EventInfo", 1.0, "EventInfo");

    view->setSource(QUrl("qrc:/src/main.qml"));

    connect(view->engine(), SIGNAL(quit()), qApp, SLOT(quit()));

#if defined(Q_WS_WIN)
    resize(400, 540);
#endif
}

CuteUp::~CuteUp()
{
}

void CuteUp::setOrientation(ScreenOrientation orientation)
{
#ifdef Q_OS_SYMBIAN
    if (orientation != ScreenOrientationAuto) {
#if defined(ORIENTATIONLOCK)
        const CAknAppUiBase::TAppUiOrientation uiOrientation =
                (orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
                    : CAknAppUi::EAppUiOrientationLandscape;
        CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
        TRAPD(error,
            if (appUi)
                appUi->SetOrientationL(uiOrientation);
        );
        Q_UNUSED(error)
#else // ORIENTATIONLOCK
        qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
#endif // ORIENTATIONLOCK
    }
#elif defined(Q_WS_MAEMO_5)
    Qt::WidgetAttribute attribute;
    switch (orientation) {
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_Maemo5PortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_Maemo5LandscapeOrientation;
        break;
    case ScreenOrientationAuto:
    default:
        attribute = Qt::WA_Maemo5AutoOrientation;
        break;
    }
    setAttribute(attribute, true);
#else // Q_OS_SYMBIAN
    Q_UNUSED(orientation);
#endif // Q_OS_SYMBIAN
}

void CuteUp::showExpanded()
{
#ifdef Q_OS_SYMBIAN
    showFullScreen();
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    showMaximized();
#else
    show();
#endif
}
